package edu.yctc.manage.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.yctc.manage.constant.ResultCode;
import edu.yctc.manage.dao.UserDAO;
import edu.yctc.manage.entity.ResultDO;
import edu.yctc.manage.entity.UserDO;
import edu.yctc.manage.service.UserService;
import edu.yctc.manage.utils.SHA256Util;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDAO, UserDO> implements UserService {

    private final static Logger LOG = LoggerFactory.getLogger("serviceLogger");

    @Resource
    private UserDAO userDAO;

    @Override public ResultDO signIn(UserDO userDO) {
        if (StringUtils.isBlank(userDO.getAccount()) || StringUtils.isBlank(userDO.getPassword())) {
            LOG.error("parameter invalid, userDO={}", userDO);
            return ResultDO.error(ResultCode.PARAMETER_INVALID, ResultCode.MSG_PARAMETER_INVALID);
        }
        UserDO toGet = userDAO.selectByAccountAndPassword(userDO.getAccount(), SHA256Util.SHA256(userDO.getPassword()));
        if (toGet == null) {
            LOG.error("no such user, userDO={}", userDO);
            return ResultDO.error(ResultCode.ADMIN_NOT_EXIST, ResultCode.MSG_ADMIN_NOT_EXIST);
        }
        LOG.info("sign in success, userDO={}", userDO);
        return ResultDO.ok();

    }

}